import { director, instantiate, Prefab, UIOpacity } from 'cc';
import { _decorator, Component, Node, UITransform, EventTouch, Vec2, Vec3, v3, Label, JsonAsset, v2 } from 'cc';
import { EventDispatcher } from '../../../../ngame/util/EventDispatcher';
import GameConst, { NPrefabConst } from '../../../GameConst';
import NWorld2Component from '../../../ngame/sync/NWorld2Component';
import { CardsController } from './CardsController';
const { ccclass, property } = _decorator;

class CardConfig {
    id?:number;
    title?:string;
    content?:string;
    isShowScene?:boolean
}

@ccclass('CardController')
export class CardController extends Component {

    //默认坐标点
    defaultPos:Vec3 = null;

    //是否被操作
    isControl:boolean = false;

    //index
    index:number = 0;

    //父节点
    cards:CardsController = null;

    isOpen:boolean = false;

    private offset:Vec3 = v3();
    private vec3:Vec3 = v3();

    //标题
    @property({ type:Label, displayName:"标题" })
    title:Label = null;

    //内容
    @property({ type:Label, displayName:"内容" })
    content:Label = null;

    //卡片配置
    @property({ type:JsonAsset, displayName:"卡片配置" })
    configs:JsonAsset = null;

    _config:CardConfig = {};
    get config(){return this._config}
    set config(value){
        this._config = value;
        this.updateConfig();
    };

    //卡片类型
    type:string = "Add_Soldier";

    //场景预览Node
    viewScene:Node;
    //场景预览Prefab
    viewScenePrefab:Prefab;
    

    onLoad() {

        this.defaultPos = this.node.position.clone();
        this.onEvent();
        
        if(this.configs && this.configs.json){
            console.log("加载卡牌");
            let config = this.configs.json[this.type];
            this.config = {...config};
        }

    }

    //更新配置
    async updateConfig(){

        this.title.string = this.config.title || '无标题';
        this.content.string = this.config.content || '无内容';

        if(NPrefabConst[this.config.id]){
            this.viewScenePrefab = await NPrefabConst[this.config.id].getPrefab();
        }


    }

    onEvent(){

        this.node.on(Node.EventType.TOUCH_START,this.onTouchStart,this);
        this.node.on(Node.EventType.TOUCH_MOVE,this.onTouchMove,this);
        this.node.on(Node.EventType.TOUCH_END,this.onTouchEnd,this);
        this.node.on(Node.EventType.TOUCH_CANCEL,this.onTouchEnd,this);
        
    }

    update(deltaTime: number) {

        if(!this.isControl && this.defaultPos){

            Vec3.add(this.vec3,this.offset,this.defaultPos.clone());
            this.node.setPosition(this.node.position.clone().lerp(this.vec3,deltaTime * 4));

        }

    }

    onClickCard(){

        if(!this.isOpen)
            this.cards.openCard(this.index);
        else
            this.cards.updateCards();

    }

    onTouchStart(event:EventTouch){

        this.isControl = true;

    }

    onTouchMove(event:EventTouch){
        this.node.getComponent(UIOpacity).opacity = 255;

        if(this.isControl){
            Object.assign(this.node.position,{
                x:this.node.position.x + event.getUIDelta().x,
                y:this.node.position.y + event.getUIDelta().y,
            })
        }

        
        //场景显示
        if(this.cards){
            let ui = this.cards.getComponent(UITransform);
            if(!ui.isHit(event.getUILocation())){
                this.onShowScene(event);
            }
        }

        // //场景提示
        // if(this.config.isShowScene){

        //     //在场景显示

        // }
        

    }

    onTouchEnd(event:EventTouch){

        this.isControl = false;
        
        
        //判断是否超出父节点
        if(this.cards){
            let ui = this.cards.getComponent(UITransform);
            if(!ui.isHit(event.getUILocation())){
                this.onUseCard(event);
            }
        }

        if(this.viewScene){
            this.viewScene.destroy();
            this.viewScene = null;
        }

        this.node.getComponent(UIOpacity).opacity = 255;
        

    }
    
    // onCardsTouchCancel(event:EventTouch){
    //     console.log("onCardsTouchCancel - 拿着卡牌移出卡牌堆");
    //     this.onUseCard(event.getUILocation());
    // }

    //使用卡牌
    onUseCard(event){
        console.log("onUseCard - 使用卡牌",this.type);
        this[`${this.type}`](this,event);
    }
    
    //场景显示
    onShowScene(event){

        if(GameConst.TOUCHS_MANAGE){
            let result = GameConst.TOUCHS_MANAGE.getUIWorldPosition(event);
            if(result)
                this[`Scene_${this.type}`] && this[`Scene_${this.type}`](result.hitPoint.clone());
        }

    }


    //添加士兵
    Add_Soldier(card:CardController,event){

        

    }

    //场景显示树木
    Scene_Add_Tree(wPos:Vec3){
        
        if(!this.viewScenePrefab) return;

        this.node.getComponent(UIOpacity).opacity = 0;

        if(!this.viewScene){
            //初始化Node
            this.viewScene = instantiate(this.viewScenePrefab);
            if(this.viewScene.getComponent(NWorld2Component)){
                this.viewScene.getComponent(NWorld2Component).isSync = false;
            }
            director.getScene().addChild(this.viewScene);
        }

        this.viewScene.worldPosition = v3(wPos.x,0,wPos.z);

    }

    //添加树木
    Add_Tree(card:CardController,event){

        let wPos = GameConst.TOUCHS_MANAGE.getUIWorldPosition(event).hitPoint;

        GameConst.I_SCENE.rServerEvent("onPlaceTrees",GameConst.getUserId(),v3(wPos.x,0,wPos.z));

    }

}

